package com.tech.connect.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.dialog.ItemsDialog;
import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.GridOffsetsItemDecoration;
import com.tech.connect.R;

import java.util.ArrayList;
import java.util.List;

public class NearActivity extends BaseActivity {

    private BaseAdapter<String, BaseHolder> adapter;
    private RecyclerView recycler;
    private List<String> allList = new ArrayList<>();
    private int height;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("附近的人").setRightIcon(R.drawable.icon_more, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ItemsDialog itemsDialog = new ItemsDialog<>(activity, new String[]{"只看男生", "只看女生", "查看全部"}, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        dialog.dismiss();
                        if (position != -1) {
                            showToast("pos" + position);
                        }
                    }
                });
                itemsDialog.show();
            }
        });
        setContentView(R.layout.layout_recycler);
        initView();
        loadData();
    }


    private void initView() {
        int screenWidth = AndroidUtil.getScreenWidth(activity);
        height = (screenWidth - AndroidUtil.dip2px(50)) / 2;
        recycler = findViewById(R.id.recycler);
        recycler.setLayoutManager(new GridLayoutManager(activity, 2) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        });
        GridOffsetsItemDecoration itemDecoration = new GridOffsetsItemDecoration(GridOffsetsItemDecoration.GRID_OFFSETS_VERTICAL);
        itemDecoration.setHorizontalItemOffsets(AndroidUtil.dip2px(10));
        itemDecoration.setVerticalItemOffsets(AndroidUtil.dip2px(10));
        recycler.addItemDecoration(itemDecoration);
    }


    private void initAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<String, BaseHolder>(R.layout.item_activity_near, allList, 6) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ImageView ivImage = helper.getView(R.id.ivImage);
                    ImageLoader.getInstance().load(activity, R.drawable.common_icon_image_placeholder, ivImage, new RequestOptions().override(height));
                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }


    private void loadData() {
        initAdapter();
    }

}